from typing import TypeVar
from pydantic.dataclasses import dataclass
from sanic.response import JSONResponse
from typing import Dict, Optional
from pydantic import RootModel

T = TypeVar('T')

@dataclass
class Res:
    data: T = None

def json(
    body: Res,
    status: int = 200,
    headers: Optional[Dict[str, str]] = None,
) -> JSONResponse:
    """Returns response object with body in json format.

    Args:
        body (Res): Response data to be Res.
        status (int, optional): HTTP response code. Defaults to `200`.
        headers (Dict[str, str], optional): Custom HTTP headers. Defaults to `None`.
    Returns:
        JSONResponse: A response object with body in json format.
    """  # noqa: E501
    return JSONResponse(
        body,
        status=status,
        headers=headers,
        content_type="application/json",
        dumps=RootModel.model_dump_json,
    )
